package com.example.anliku.service.serviceImpl;

import cn.hutool.core.collection.CollectionUtil;
import com.example.anliku.entity.ExampleClazz;
import com.example.anliku.entity.Message;
import com.example.anliku.entity.clazzBody.clazzRetBody;
import com.example.anliku.mapper.ExampleClazzMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.example.anliku.mapper.ClazzMapper;
import com.example.anliku.entity.Clazz;
import com.example.anliku.service.ClazzService;

import java.util.ArrayList;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Resource
    private ClazzMapper clazzMapper;
    @Resource
    private ExampleClazzMapper exampleClazzMapper;

    @Override
    public Message deleteByPrimaryKey(Integer clazzId) {
        List<String> clazz1 = clazzMapper.selectClazzNameByClazzId(clazzId);
        int a = exampleClazzMapper.deleteByExampleClazzClazzName(clazz1.get(0));
        int b = clazzMapper.deleteByPrimaryKey(clazzId);
        if (a > 0 || b > 0) {
            return Message.success(null);
        } else {
            return Message.fail(null);
        }
    }

    @Override
    public Message insert(Clazz record) {
        return clazzMapper.insert(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insertSelective(Clazz record) {
       List <Clazz> clazz=clazzMapper.selectAllByClazzName(record.getClazzName());
        if(CollectionUtil.isNotEmpty(clazz)){
            return Message.fail("课程标签重复");
        }
        return clazzMapper.insertSelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer clazzId) {
        return Message.success(null).add(clazzMapper.selectByPrimaryKey(clazzId));
    }

    @Override
    public Message updateByPrimaryKeySelective(Clazz record) {
        return clazzMapper.updateByPrimaryKeySelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message updateByPrimaryKey(Clazz record) {
        return Message.success(null).add(clazzMapper.updateByPrimaryKey(record));
    }

    @Override
    public Message selectAll() {
        return Message.success(null).add(clazzMapper.selectAll());
    }

    @Override
    public Message insertList(List<Clazz> list) {
        return clazzMapper.insertList(list) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message listtabBody(List<clazzRetBody> list) {
        List<Clazz> clazzLsit = new ArrayList<>();
        for (clazzRetBody clazzRetBody : list) {
            List<Clazz> a = clazzMapper.selectAllByClazzName(clazzRetBody.getClazzName());
            if (CollectionUtils.isNotEmpty(a)) {
                return Message.fail("课程名添加重复");
            }
            clazzLsit.add(Clazz.get(clazzRetBody));
        }
        return clazzMapper.batchInsert(clazzLsit) > 0 ? Message.success(null) : Message.fail(null);
    }
}


